package com.and9.mchen.mweather.cw.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * 汉字转换位汉语拼音，英文字符不变
 * @author MC
 *
 */
public class Cn2SpellUtil {
 
	private final static Log log=LogFactory.getLog(Cn2SpellUtil.class);
	
	/**
	 * 
	 * @param chines
	 * @return
	 */
    public static String converterToFirstSpell(String chines){
    	
    	if(chines==null || chines.equals(""))
    		return chines;
        
    	StringBuilder pinyinName = new StringBuilder();
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        String[] temp=null;
        
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                	temp=PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
                	if(temp!=null)
                		pinyinName.append(temp[0].charAt(0));
                	else
                		pinyinName.append(nameChar[i]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                	log.error("汉字转首拼音出错："+e);
                    return null;
                }
            }else{
            	pinyinName.append(nameChar[i]);
            }
        }
        return pinyinName.toString();
    }
 
    /**
     * 
     * @param chines
     * @return
     */
    public static String converterToSpell(String chines){
    	
    	if(chines==null || chines.equals(""))
    		return chines;
        
    	StringBuilder pinyinName = new StringBuilder();
        char[] nameChar = chines.toCharArray();
        String[] temp=null;
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                	temp=PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
                	if(temp!=null)
                		pinyinName.append(temp[0]);
                	else
                		pinyinName.append(nameChar[i]);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                	log.error("汉字转首拼音出错："+e);
                	return null;
                }
            }else{
            	pinyinName.append(nameChar[i]);
            }
        }
        return pinyinName.toString();
    }
    
}